/**
 * Copyright (C) 2012 by team 4 M11CQK02-B (Khoa hoc may tinh 2011 - PTIT)
 * 
 * Advanced Algorithm Report
 */
package com.team4.AdvancedAlgorithm.alg;

import java.util.ArrayList;

/**
 * @author <a href="mailto:ndkhoi168@gmail.com">Nguyen Duc Khoi</a>
 * Jan 9, 2012
 */
public class SubnetAlgorithm
{
   private int x, y, z, v, m, k;
   
   private int bitMin, bitMax;

   private String message = "";

   private ArrayList<RowValue> solutions;

   public SubnetAlgorithm(int x, int y, int z, int v, int m, int k)
   {
      this.x = x;
      this.y = y;
      this.z = z;
      this.v = v;
      this.m = m;
      this.k = k;
      this.solutions = new ArrayList<SubnetAlgorithm.RowValue>();
   }

   public boolean process()
   {
      bitMin = (int) Math.ceil(Math.log(m + 2) / Math.log(2));
      int netID = Utils.calculateNetID(x);
      bitMax = (int) Math.floor(32 - netID - Math.log(k + 2) / Math.log(2));
      if (bitMin > bitMax)
      {
         message = "There are no solution found";
         return false;
      }

      for (int i = bitMin; i <= bitMax; i++)
      {
         int borrowingBits = i;
         int remainingBits = 32 - netID - i;
         int netNumbers = (int) (Math.pow(2, i) - 2);
         int hostNumbers = (int) (Math.pow(2, remainingBits) - 2);
         RowValue row = new RowValue(borrowingBits, remainingBits, netNumbers, hostNumbers);
         solutions.add(row);
      }
      return true;
   }

   public String getMessage()
   {
      return message;
   }

   public void setMessage(String message)
   {
      this.message = message;
   }

   public ArrayList<RowValue> getSolutions()
   {
      return solutions;
   }

   public void setSolutions(ArrayList<RowValue> solutions)
   {
      this.solutions = solutions;
   }

   public class RowValue
   {
      private final int borrowingBits, remainingBits, netNumbers, hostNumbers;

      public RowValue(int borrowingBits, int remainingBits, int netNumbers, int hostNumbers)
      {
         this.borrowingBits = borrowingBits;
         this.remainingBits = remainingBits;
         this.netNumbers = netNumbers;
         this.hostNumbers = hostNumbers;
      }

      public int getBorrowingBits()
      {
         return borrowingBits;
      }

      public int getRemainingBits()
      {
         return remainingBits;
      }

      public int getNetNumbers()
      {
         return netNumbers;
      }

      public int getHostNumbers()
      {
         return hostNumbers;
      }
   }

   public int getX()
   {
      return x;
   }

   public void setX(int x)
   {
      this.x = x;
   }

   public int getY()
   {
      return y;
   }

   public void setY(int y)
   {
      this.y = y;
   }

   public int getZ()
   {
      return z;
   }

   public void setZ(int z)
   {
      this.z = z;
   }

   public int getV()
   {
      return v;
   }

   public void setV(int v)
   {
      this.v = v;
   }

   public int getM()
   {
      return m;
   }

   public void setM(int m)
   {
      this.m = m;
   }

   public int getK()
   {
      return k;
   }

   public void setK(int k)
   {
      this.k = k;
   }

   public int getBitMin()
   {
      return bitMin;
   }

   public void setBitMin(int bitMin)
   {
      this.bitMin = bitMin;
   }

   public int getBitMax()
   {
      return bitMax;
   }

   public void setBitMax(int bitMax)
   {
      this.bitMax = bitMax;
   }
}
